﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TcpLabCommon;
namespace Common
{
    /// <summary>

    /// 复数类
    /// </summary>
    public class Complex
    {



        /// <summary>

        /// 默认构造函数
        /// </summary>

        public Complex()

            : this(0, 0)
        {

        }



        /// <summary>

        /// 只有实部的构造函数
        /// </summary>

        /// <param name="real">实部</param>

        public Complex(double real)

            : this(real, 0) { }



        /// <summary>

        /// 由实部和虚部构造
        /// </summary>

        /// <param name="real">实部</param>

        /// <param name="image">虚部</param>

        public Complex(double real, double image)
        {

            this.real = real;

            this.image = image;

        }



        private double real;

        /// <summary>

        /// 复数的实部
        /// </summary>

        public double Real
        {

            get { return real; }

            set { real = value; }

        }



        private double image;

        /// <summary>

        /// 复数的虚部
        /// </summary>

        public double Image
        {

            get { return image; }

            set { image = value; }

        }



        ///重载加法
        public static Complex operator +(Complex c1, Complex c2)
        {

            return new Complex(c1.real + c2.real, c1.image + c2.image);

        }



        ///重载减法
        public static Complex operator -(Complex c1, Complex c2)
        {

            return new Complex(c1.real - c2.real, c1.image - c2.image);

        }



        ///重载乘法
        public static Complex operator *(Complex c1, Complex c2)
        {

            return new Complex(c1.real * c2.real - c1.image * c2.image, c1.image * c2.real + c1.real * c2.image);

        }



        /// <summary>

        /// 求复数的模
        /// </summary>

        /// <returns>模</returns>

        public double ToModul()
        {

            return Math.Sqrt(real * real + image * image);

        }



        /// <summary>

        /// 重载ToString方法
        /// </summary>

        /// <returns>打印字符串</returns>

        public override string ToString()
        {

            if (Real == 0 && Image == 0)
            {

                return string.Format("{0}", 0);

            }

            if (Real == 0 && (Image != 1 && Image != -1))
            {

                return string.Format("{0} i", Image);

            }

            if (Image == 0)
            {

                return string.Format("{0}", Real);

            }

            if (Image == 1)
            {

                return string.Format("i");

            }

            if (Image == -1)
            {

                return string.Format("- i");

            }

            if (Image < 0)
            {

                return string.Format("{0} - {1} i", Real, -Image);

            }

            return string.Format("{0} + {1} i", Real, Image);

        }

    }


    /// <summary>

    /// 频率分析器
    /// </summary>
    public static class FFTCalc
    {
        /// <summary>

        /// 求复数complex数组的模modul数组
        /// </summary>

        /// <param name="input">复数数组</param>

        /// <returns>模数组</returns>

        public static double[] Cmp2Mdl(Complex[] input)
        {

            ///有输入数组的长度确定输出数组的长度
            double[] output = new double[input.Length];



            ///对所有输入复数求模
            for (int i = 0; i < input.Length; i++)
            {

                if (input[i].Real > 0)
                {

                    output[i] = -input[i].ToModul();

                }

                else
                {

                    output[i] = input[i].ToModul();

                }

            }



            ///返回模数组
            return output;

        }

        /// <summary>

        /// 傅立叶变换或反变换，递归实现多级蝶形运算
        /// 作为反变换输出需要再除以序列的长度
        /// ！注意：输入此类的序列长度必须是2^n

        /// </summary>

        /// <param name="input">输入实序列</param>

        /// <param name="invert">false=正变换，true=反变换</param>

        /// <returns>傅立叶变换或反变换后的序列</returns>

        public static Complex[] FFT1(double[] input, bool invert)
        {
            ///由输入序列确定输出序列的长度
            Complex[] output = new Complex[input.Length];

            ///将输入的实数转为复数
            for (int i = 0; i < input.Length; i++)
            {
                output[i] = new Complex(input[i]);
            }
            ///返回FFT或IFFT后的序列
            return output = FFT(output, invert);
        }

        /// <summary>

        /// 傅立叶变换或反变换，递归实现多级蝶形运算
        /// 作为反变换输出需要再除以序列的长度
        /// ！注意：输入此类的序列长度必须是2^n

        /// </summary>

        /// <param name="input">复数输入序列</param>

        /// <param name="invert">false=正变换，true=反变换</param>

        /// <returns>傅立叶变换或反变换后的序列</returns>

        private static Complex[] FFT(Complex[] input, bool invert)
        {
            ///输入序列只有一个元素，输出这个元素并返回
            if (input.Length == 1)
            {
                return new Complex[] { input[0] };
            }
            ///输入序列的长度
            int length = input.Length;

            ///输入序列的长度的一半
            int half = length / 2;

            ///有输入序列的长度确定输出序列的长度
            Complex[] output = new Complex[length];

            ///正变换旋转因子的基数
            double fac = -2.0 * Math.PI / length;

            ///反变换旋转因子的基数是正变换的相反数
            if (invert)
            {
                fac = -fac;
            }



            ///序列中下标为偶数的点
            Complex[] evens = new Complex[half];



            for (int i = 0; i < half; i++)
            {

                evens[i] = input[2 * i];

            }



            ///求偶数点FFT或IFFT的结果，递归实现多级蝶形运算
            Complex[] evenResult = FFT(evens, invert);



            ///序列中下标为奇数的点
            Complex[] odds = new Complex[half];



            for (int i = 0; i < half; i++)
            {

                odds[i] = input[2 * i + 1];

            }



            ///求偶数点FFT或IFFT的结果，递归实现多级蝶形运算
            Complex[] oddResult = FFT(odds, invert);



            for (int k = 0; k < half; k++)
            {

                ///旋转因子
                double fack = fac * k;



                ///进行蝶形运算
                Complex oddPart = oddResult[k] * new Complex(Math.Cos(fack), Math.Sin(fack));

                output[k] = evenResult[k] + oddPart;

                output[k + half] = evenResult[k] - oddPart;

            }



            ///返回FFT或IFFT的结果
            return output;

        }

        /// <summary>

        /// 频域滤波器
        /// </summary>

        /// <param name="data">待滤波的数据</param>

        /// <param name="Nc">滤波器截止波数</param>

        /// <param name="Hd">滤波器的权函数</param>

        /// <returns>滤波后的数据</returns>

        private static double[] FreqFilter(double[] data, int Nc, Complex[] Hd)
        {

            ///最高波数==数据长度
            int N = data.Length;

            ///输入数据进行FFT

            Complex[] fData = FFTCalc.FFT1(data, false);



            ///频域滤波
            for (int i = 0; i < N; i++)
            {

                fData[i] = Hd[i] * fData[i];

            }



            ///滤波后数据计算IFFT

            ///！未除以数据长度
            fData = FFTCalc.FFT(fData, true);



            ///复数转成模
            data = FFTCalc.Cmp2Mdl(fData);



            ///除以数据长度
            for (int i = 0; i < N; i++)
            {

                data[i] = -data[i] / N;

            }



            ///返回滤波后的数据
            return data;

        }

        public static double[] AMP1(double[] data)
        {

            ///最高波数==数据长度
            int N = data.Length;

            ///输入数据进行FFT

            Complex[] fData = FFTCalc.FFT1(data, false);

            double[] amp = new double[N / 2];



            for (int i = 0; i < N / 2; i++)
            {
                double temp = fData[i].Real * fData[i].Real + fData[i].Image * fData[i].Image;
                temp = Math.Sqrt(temp);
                double temp1 = Math.Atan2(fData[i].Image, fData[i].Real);
                if (i == 0)
                {
                    amp[i] = temp / 1024.0;
                }
                else
                {
                    amp[i] = Math.Abs(2 * temp / 1024.0 * Math.Cos(2 * Math.PI * (i * i / 1024.0) + temp1));
                }
            }
            return amp;
        }

        public static double[] AMP2(double[] data)
        {
            ///最高波数==数据长度
            int N = data.Length;

            ///输入数据进行FFT
            Complex[] fData = FFTCalc.FFT1(data, false);

            double[] amp = new double[N / 2];

            for (int i = 0; i < N / 2; i++)
            {
                double temp = fData[i].Real * fData[i].Real + fData[i].Image * fData[i].Image;
                temp = Math.Sqrt(temp);
                temp = temp / (N / 2);
                amp[i] = temp;
            }
            return amp;
        }
        public static FFTStruct fftStruct(double[] data)
        {
            ///最高波数==数据长度
            int N = data.Length;

            ///输入数据进行FFT
            Complex[] fData = FFTCalc.FFT1(data, false);

            double[] amp = new double[N / 2];
            double[] Phase = new double[N / 2];
            for (int i = 0; i < N / 2; i++)
            {
                double temp = fData[i].Real * fData[i].Real + fData[i].Image * fData[i].Image;
                temp = Math.Sqrt(temp);
                temp = temp / (N / 2);
                amp[i] = temp;
                double ph = Math.Atan2(fData[i].Image, fData[i].Real);
                Phase[i] = ph * 180 / Math.PI;
            }

            FFTStruct STRUCT = new FFTStruct();
            STRUCT.AMP = amp;
            STRUCT.Phase = Phase;
            return STRUCT;
        }


        #region 优化过的FFT

        public static byte TRUE = 1;
        public static byte FALSE = 0;


        #region 全局变量
        #endregion


        //fft旋转英子
        public static void FFTCalcnew(float[] x, creal_T[] y)
        {
            creal_T[] b_y1 = new creal_T[1024];
            Int32 ix;
            Int32 ju;
            Int32 iy;
            Int32 i;
            byte tst;
            double temp_re;
            double temp_im;
            Int32 iDelta = 0;
            Int32 iDelta2 = 0;
            Int32 k = 0;
            Int32 iheight = 0;
            double[] dv0 = new double[513]{ 0.0, -0.0061358846491544753
    ,-0.012271538285719925, -0.01840672990580482, -0.024541228522912288,
    -0.030674803176636626, -0.036807222941358832, -0.04293825693494082,
    -0.049067674327418015, -0.055195244349689941, -0.061320736302208578,
    -0.067443919563664051, -0.073564563599667426, -0.079682437971430126,
    -0.0857973123444399, -0.091908956497132724, -0.0980171403295606,
    -0.10412163387205459, -0.11022220729388306, -0.11631863091190475,
    -0.1224106751992162, -0.12849811079379317, -0.13458070850712617,
    -0.14065823933284921, -0.14673047445536175, -0.15279718525844344,
    -0.15885814333386145, -0.16491312048996992, -0.17096188876030122,
    -0.17700422041214875, -0.18303988795514095, -0.18906866414980619,
    -0.19509032201612825, -0.2011046348420919, -0.20711137619221856,
    -0.21311031991609136, -0.2191012401568698, -0.22508391135979283,
    -0.23105810828067111, -0.2370236059943672, -0.24298017990326387,
    -0.24892760574572015, -0.25486565960451457, -0.26079411791527551,
    -0.26671275747489837, -0.272621355449949, -0.27851968938505306,
    -0.28440753721127188, -0.29028467725446233, -0.29615088824362379,
    -0.30200594931922808, -0.30784964004153487, -0.31368174039889152,
    -0.31950203081601569, -0.32531029216226293, -0.33110630575987643,
    -0.33688985339222005, -0.34266071731199438, -0.34841868024943456,
    -0.35416352542049034, -0.35989503653498811, -0.36561299780477385,
    -0.37131719395183749, -0.37700741021641826, -0.38268343236508978,
    -0.38834504669882625, -0.3939920400610481, -0.39962419984564679,
    -0.40524131400498986, -0.41084317105790391, -0.41642956009763715,
    -0.42200027079979968, -0.42755509343028208, -0.43309381885315196,
    -0.43861623853852766, -0.4441221445704292, -0.44961132965460654,
    -0.45508358712634384, -0.46053871095824, -0.46597649576796618,
    -0.47139673682599764, -0.47679923006332209, -0.48218377207912272,
    -0.487550160148436, -0.49289819222978404, -0.49822766697278181,
    -0.50353838372571758, -0.508830142543107, -0.51410274419322166,
    -0.51935599016558964, -0.524589682678469, -0.52980362468629461,
    -0.53499761988709715, -0.54017147272989285, -0.54532498842204646,
    -0.55045797293660481, -0.55557023301960218, -0.560661576197336,
    -0.56573181078361312, -0.57078074588696726, -0.57580819141784534,
    -0.58081395809576453, -0.58579785745643886, -0.59075970185887416,
    -0.59569930449243336, -0.600616479383869, -0.60551104140432555,
    -0.61038280627630948, -0.61523159058062682, -0.6200572117632891,
    -0.62485948814238634, -0.629638238914927, -0.63439328416364549,
    -0.63912444486377573, -0.64383154288979139, -0.64851440102211244,
    -0.65317284295377676, -0.65780669329707864, -0.66241577759017178,
    -0.66699992230363747, -0.67155895484701833, -0.67609270357531592,
    -0.680600997795453, -0.68508366777270036, -0.68954054473706683,
    -0.69397146088965389, -0.69837624940897292, -0.7027547444572253,
    -0.70710678118654757, -0.71143219574521643, -0.71573082528381859,
    -0.72000250796138165, -0.724247082951467, -0.7284643904482252,
    -0.73265427167241282, -0.73681656887736979, -0.74095112535495922,
    -0.745057785441466, -0.74913639452345937, -0.75318679904361252,
    -0.75720884650648457, -0.76120238548426178, -0.765167265622459,
    -0.7691033376455797, -0.773010453362737, -0.77688846567323244,
    -0.78073722857209449, -0.78455659715557524, -0.78834642762660634,
    -0.79210657730021239, -0.79583690460888357, -0.799537269107905,
    -0.80320753148064494, -0.80684755354379933, -0.81045719825259477,
    -0.81403632970594841, -0.81758481315158371, -0.82110251499110465,
    -0.82458930278502529, -0.8280450452577558, -0.83146961230254524,
    -0.83486287498638, -0.83822470555483808, -0.84155497743689844,
    -0.84485356524970712, -0.84812034480329723, -0.8513551931052652,
    -0.85455798836540053, -0.85772861000027212, -0.86086693863776731,
    -0.8639728561215867, -0.86704624551569265, -0.87008699110871146,
    -0.87309497841829009, -0.8760700941954066, -0.87901222642863353,
    -0.881921264348355, -0.88479709843093779, -0.88763962040285393,
    -0.89044872324475788, -0.89322430119551532, -0.89596624975618522,
    -0.89867446569395382, -0.901348847046022, -0.90398929312344334,
    -0.90659570451491533, -0.90916798309052238, -0.91170603200542988,
    -0.91420975570353069, -0.9166790599210427, -0.91911385169005777,
    -0.9215140393420419, -0.92387953251128674, -0.92621024213831138,
    -0.92850608047321559, -0.93076696107898371, -0.932992798834739,
    -0.93518350993894761, -0.937339011912575, -0.93945922360218992,
    -0.94154406518302081, -0.94359345816196039, -0.94560732538052128,
    -0.94758559101774109, -0.94952818059303667, -0.95143502096900834,
    -0.95330604035419386, -0.95514116830577078, -0.95694033573220882,
    -0.9587034748958716, -0.96043051941556579, -0.96212140426904158,
    -0.96377606579543984, -0.9653944416976894, -0.96697647104485207,
    -0.96852209427441727, -0.970031253194544, -0.97150389098625178,
    -0.97293995220556018, -0.97433938278557586, -0.97570213003852857,
    -0.97702814265775439, -0.97831737071962765, -0.97956976568544052,
    -0.98078528040323043, -0.98196386910955524, -0.98310548743121629,
    -0.984210092386929, -0.98527764238894122, -0.98630809724459867,
    -0.98730141815785843, -0.98825756773074946, -0.989176509964781,
    -0.99005821026229712, -0.99090263542778, -0.99170975366909953,
    -0.99247953459871, -0.9932119492347945, -0.99390697000235606,
    -0.99456457073425542, -0.99518472667219693, -0.99576741446765982,
    -0.996312612182778, -0.99682029929116567, -0.99729045667869021,
    -0.99772306664419164, -0.99811811290014918, -0.99847558057329477,
    -0.99879545620517241, -0.99907772775264536, -0.99932238458834954,
    -0.99952941750109314, -0.99969881869620425, -0.9998305817958234,
    -0.9999247018391445, -0.99998117528260111, -1.0, -0.99998117528260111,
    -0.9999247018391445, -0.9998305817958234, -0.99969881869620425,
    -0.99952941750109314, -0.99932238458834954, -0.99907772775264536,
    -0.99879545620517241, -0.99847558057329477, -0.99811811290014918,
    -0.99772306664419164, -0.99729045667869021, -0.99682029929116567,
    -0.996312612182778, -0.99576741446765982, -0.99518472667219693,
    -0.99456457073425542, -0.99390697000235606, -0.9932119492347945,
    -0.99247953459871, -0.99170975366909953, -0.99090263542778,
    -0.99005821026229712, -0.989176509964781, -0.98825756773074946,
    -0.98730141815785843, -0.98630809724459867, -0.98527764238894122,
    -0.984210092386929, -0.98310548743121629, -0.98196386910955524,
    -0.98078528040323043, -0.97956976568544052, -0.97831737071962765,
    -0.97702814265775439, -0.97570213003852857, -0.97433938278557586,
    -0.97293995220556018, -0.97150389098625178, -0.970031253194544,
    -0.96852209427441727, -0.96697647104485207, -0.9653944416976894,
    -0.96377606579543984, -0.96212140426904158, -0.96043051941556579,
    -0.9587034748958716, -0.95694033573220882, -0.95514116830577078,
    -0.95330604035419386, -0.95143502096900834, -0.94952818059303667,
    -0.94758559101774109, -0.94560732538052128, -0.94359345816196039,
    -0.94154406518302081, -0.93945922360218992, -0.937339011912575,
    -0.93518350993894761, -0.932992798834739, -0.93076696107898371,
    -0.92850608047321559, -0.92621024213831138, -0.92387953251128674,
    -0.9215140393420419, -0.91911385169005777, -0.9166790599210427,
    -0.91420975570353069, -0.91170603200542988, -0.90916798309052238,
    -0.90659570451491533, -0.90398929312344334, -0.901348847046022,
    -0.89867446569395382, -0.89596624975618522, -0.89322430119551532,
    -0.89044872324475788, -0.88763962040285393, -0.88479709843093779,
    -0.881921264348355, -0.87901222642863353, -0.8760700941954066,
    -0.87309497841829009, -0.87008699110871146, -0.86704624551569265,
    -0.8639728561215867, -0.86086693863776731, -0.85772861000027212,
    -0.85455798836540053, -0.8513551931052652, -0.84812034480329723,
    -0.84485356524970712, -0.84155497743689844, -0.83822470555483808,
    -0.83486287498638, -0.83146961230254524, -0.8280450452577558,
    -0.82458930278502529, -0.82110251499110465, -0.81758481315158371,
    -0.81403632970594841, -0.81045719825259477, -0.80684755354379933,
    -0.80320753148064494, -0.799537269107905, -0.79583690460888357,
    -0.79210657730021239, -0.78834642762660634, -0.78455659715557524,
    -0.78073722857209449, -0.77688846567323244, -0.773010453362737,
    -0.7691033376455797, -0.765167265622459, -0.76120238548426178,
    -0.75720884650648457, -0.75318679904361252, -0.74913639452345937,
    -0.745057785441466, -0.74095112535495922, -0.73681656887736979,
    -0.73265427167241282, -0.7284643904482252, -0.724247082951467,
    -0.72000250796138165, -0.71573082528381859, -0.71143219574521643,
    -0.70710678118654757, -0.7027547444572253, -0.69837624940897292,
    -0.69397146088965389, -0.68954054473706683, -0.68508366777270036,
    -0.680600997795453, -0.67609270357531592, -0.67155895484701833,
    -0.66699992230363747, -0.66241577759017178, -0.65780669329707864,
    -0.65317284295377676, -0.64851440102211244, -0.64383154288979139,
    -0.63912444486377573, -0.63439328416364549, -0.629638238914927,
    -0.62485948814238634, -0.6200572117632891, -0.61523159058062682,
    -0.61038280627630948, -0.60551104140432555, -0.600616479383869,
    -0.59569930449243336, -0.59075970185887416, -0.58579785745643886,
    -0.58081395809576453, -0.57580819141784534, -0.57078074588696726,
    -0.56573181078361312, -0.560661576197336, -0.55557023301960218,
    -0.55045797293660481, -0.54532498842204646, -0.54017147272989285,
    -0.53499761988709715, -0.52980362468629461, -0.524589682678469,
    -0.51935599016558964, -0.51410274419322166, -0.508830142543107,
    -0.50353838372571758, -0.49822766697278181, -0.49289819222978404,
    -0.487550160148436, -0.48218377207912272, -0.47679923006332209,
    -0.47139673682599764, -0.46597649576796618, -0.46053871095824,
    -0.45508358712634384, -0.44961132965460654, -0.4441221445704292,
    -0.43861623853852766, -0.43309381885315196, -0.42755509343028208,
    -0.42200027079979968, -0.41642956009763715, -0.41084317105790391,
    -0.40524131400498986, -0.39962419984564679, -0.3939920400610481,
    -0.38834504669882625, -0.38268343236508978, -0.37700741021641826,
    -0.37131719395183749, -0.36561299780477385, -0.35989503653498811,
    -0.35416352542049034, -0.34841868024943456, -0.34266071731199438,
    -0.33688985339222005, -0.33110630575987643, -0.32531029216226293,
    -0.31950203081601569, -0.31368174039889152, -0.30784964004153487,
    -0.30200594931922808, -0.29615088824362379, -0.29028467725446233,
    -0.28440753721127188, -0.27851968938505306, -0.272621355449949,
    -0.26671275747489837, -0.26079411791527551, -0.25486565960451457,
    -0.24892760574572015, -0.24298017990326387, -0.2370236059943672,
    -0.23105810828067111, -0.22508391135979283, -0.2191012401568698,
    -0.21311031991609136, -0.20711137619221856, -0.2011046348420919,
    -0.19509032201612825, -0.18906866414980619, -0.18303988795514095,
    -0.17700422041214875, -0.17096188876030122, -0.16491312048996992,
    -0.15885814333386145, -0.15279718525844344, -0.14673047445536175,
    -0.14065823933284921, -0.13458070850712617, -0.12849811079379317,
    -0.1224106751992162, -0.11631863091190475, -0.11022220729388306,
    -0.10412163387205459, -0.0980171403295606, -0.091908956497132724,
    -0.0857973123444399, -0.079682437971430126, -0.073564563599667426,
    -0.067443919563664051, -0.061320736302208578, -0.055195244349689941,
    -0.049067674327418015, -0.04293825693494082, -0.036807222941358832,
    -0.030674803176636626, -0.024541228522912288, -0.01840672990580482,
    -0.012271538285719925, -0.0061358846491544753, -0.0 };

            double[] dv1 = new double[513] { 1.0, 0.99998117528260111, 0.9999247018391445,
    0.9998305817958234, 0.99969881869620425, 0.99952941750109314,
    0.99932238458834954, 0.99907772775264536, 0.99879545620517241,
    0.99847558057329477, 0.99811811290014918, 0.99772306664419164,
    0.99729045667869021, 0.99682029929116567, 0.996312612182778,
    0.99576741446765982, 0.99518472667219693, 0.99456457073425542,
    0.99390697000235606, 0.9932119492347945, 0.99247953459871,
    0.99170975366909953, 0.99090263542778, 0.99005821026229712,
    0.989176509964781, 0.98825756773074946, 0.98730141815785843,
    0.98630809724459867, 0.98527764238894122, 0.984210092386929,
    0.98310548743121629, 0.98196386910955524, 0.98078528040323043,
    0.97956976568544052, 0.97831737071962765, 0.97702814265775439,
    0.97570213003852857, 0.97433938278557586, 0.97293995220556018,
    0.97150389098625178, 0.970031253194544, 0.96852209427441727,
    0.96697647104485207, 0.9653944416976894, 0.96377606579543984,
    0.96212140426904158, 0.96043051941556579, 0.9587034748958716,
    0.95694033573220882, 0.95514116830577078, 0.95330604035419386,
    0.95143502096900834, 0.94952818059303667, 0.94758559101774109,
    0.94560732538052128, 0.94359345816196039, 0.94154406518302081,
    0.93945922360218992, 0.937339011912575, 0.93518350993894761,
    0.932992798834739, 0.93076696107898371, 0.92850608047321559,
    0.92621024213831138, 0.92387953251128674, 0.9215140393420419,
    0.91911385169005777, 0.9166790599210427, 0.91420975570353069,
    0.91170603200542988, 0.90916798309052238, 0.90659570451491533,
    0.90398929312344334, 0.901348847046022, 0.89867446569395382,
    0.89596624975618522, 0.89322430119551532, 0.89044872324475788,
    0.88763962040285393, 0.88479709843093779, 0.881921264348355,
    0.87901222642863353, 0.8760700941954066, 0.87309497841829009,
    0.87008699110871146, 0.86704624551569265, 0.8639728561215867,
    0.86086693863776731, 0.85772861000027212, 0.85455798836540053,
    0.8513551931052652, 0.84812034480329723, 0.84485356524970712,
    0.84155497743689844, 0.83822470555483808, 0.83486287498638,
    0.83146961230254524, 0.8280450452577558, 0.82458930278502529,
    0.82110251499110465, 0.81758481315158371, 0.81403632970594841,
    0.81045719825259477, 0.80684755354379933, 0.80320753148064494,
    0.799537269107905, 0.79583690460888357, 0.79210657730021239,
    0.78834642762660634, 0.78455659715557524, 0.78073722857209449,
    0.77688846567323244, 0.773010453362737, 0.7691033376455797,
    0.765167265622459, 0.76120238548426178, 0.75720884650648457,
    0.75318679904361252, 0.74913639452345937, 0.745057785441466,
    0.74095112535495922, 0.73681656887736979, 0.73265427167241282,
    0.7284643904482252, 0.724247082951467, 0.72000250796138165,
    0.71573082528381859, 0.71143219574521643, 0.70710678118654757,
    0.7027547444572253, 0.69837624940897292, 0.69397146088965389,
    0.68954054473706683, 0.68508366777270036, 0.680600997795453,
    0.67609270357531592, 0.67155895484701833, 0.66699992230363747,
    0.66241577759017178, 0.65780669329707864, 0.65317284295377676,
    0.64851440102211244, 0.64383154288979139, 0.63912444486377573,
    0.63439328416364549, 0.629638238914927, 0.62485948814238634,
    0.6200572117632891, 0.61523159058062682, 0.61038280627630948,
    0.60551104140432555, 0.600616479383869, 0.59569930449243336,
    0.59075970185887416, 0.58579785745643886, 0.58081395809576453,
    0.57580819141784534, 0.57078074588696726, 0.56573181078361312,
    0.560661576197336, 0.55557023301960218, 0.55045797293660481,
    0.54532498842204646, 0.54017147272989285, 0.53499761988709715,
    0.52980362468629461, 0.524589682678469, 0.51935599016558964,
    0.51410274419322166, 0.508830142543107, 0.50353838372571758,
    0.49822766697278181, 0.49289819222978404, 0.487550160148436,
    0.48218377207912272, 0.47679923006332209, 0.47139673682599764,
    0.46597649576796618, 0.46053871095824, 0.45508358712634384,
    0.44961132965460654, 0.4441221445704292, 0.43861623853852766,
    0.43309381885315196, 0.42755509343028208, 0.42200027079979968,
    0.41642956009763715, 0.41084317105790391, 0.40524131400498986,
    0.39962419984564679, 0.3939920400610481, 0.38834504669882625,
    0.38268343236508978, 0.37700741021641826, 0.37131719395183749,
    0.36561299780477385, 0.35989503653498811, 0.35416352542049034,
    0.34841868024943456, 0.34266071731199438, 0.33688985339222005,
    0.33110630575987643, 0.32531029216226293, 0.31950203081601569,
    0.31368174039889152, 0.30784964004153487, 0.30200594931922808,
    0.29615088824362379, 0.29028467725446233, 0.28440753721127188,
    0.27851968938505306, 0.272621355449949, 0.26671275747489837,
    0.26079411791527551, 0.25486565960451457, 0.24892760574572015,
    0.24298017990326387, 0.2370236059943672, 0.23105810828067111,
    0.22508391135979283, 0.2191012401568698, 0.21311031991609136,
    0.20711137619221856, 0.2011046348420919, 0.19509032201612825,
    0.18906866414980619, 0.18303988795514095, 0.17700422041214875,
    0.17096188876030122, 0.16491312048996992, 0.15885814333386145,
    0.15279718525844344, 0.14673047445536175, 0.14065823933284921,
    0.13458070850712617, 0.12849811079379317, 0.1224106751992162,
    0.11631863091190475, 0.11022220729388306, 0.10412163387205459,
    0.0980171403295606, 0.091908956497132724, 0.0857973123444399,
    0.079682437971430126, 0.073564563599667426, 0.067443919563664051,
    0.061320736302208578, 0.055195244349689941, 0.049067674327418015,
    0.04293825693494082, 0.036807222941358832, 0.030674803176636626,
    0.024541228522912288, 0.01840672990580482, 0.012271538285719925,
    0.0061358846491544753, 0.0, -0.0061358846491544753, -0.012271538285719925,
    -0.01840672990580482, -0.024541228522912288, -0.030674803176636626,
    -0.036807222941358832, -0.04293825693494082, -0.049067674327418015,
    -0.055195244349689941, -0.061320736302208578, -0.067443919563664051,
    -0.073564563599667426, -0.079682437971430126, -0.0857973123444399,
    -0.091908956497132724, -0.0980171403295606, -0.10412163387205459,
    -0.11022220729388306, -0.11631863091190475, -0.1224106751992162,
    -0.12849811079379317, -0.13458070850712617, -0.14065823933284921,
    -0.14673047445536175, -0.15279718525844344, -0.15885814333386145,
    -0.16491312048996992, -0.17096188876030122, -0.17700422041214875,
    -0.18303988795514095, -0.18906866414980619, -0.19509032201612825,
    -0.2011046348420919, -0.20711137619221856, -0.21311031991609136,
    -0.2191012401568698, -0.22508391135979283, -0.23105810828067111,
    -0.2370236059943672, -0.24298017990326387, -0.24892760574572015,
    -0.25486565960451457, -0.26079411791527551, -0.26671275747489837,
    -0.272621355449949, -0.27851968938505306, -0.28440753721127188,
    -0.29028467725446233, -0.29615088824362379, -0.30200594931922808,
    -0.30784964004153487, -0.31368174039889152, -0.31950203081601569,
    -0.32531029216226293, -0.33110630575987643, -0.33688985339222005,
    -0.34266071731199438, -0.34841868024943456, -0.35416352542049034,
    -0.35989503653498811, -0.36561299780477385, -0.37131719395183749,
    -0.37700741021641826, -0.38268343236508978, -0.38834504669882625,
    -0.3939920400610481, -0.39962419984564679, -0.40524131400498986,
    -0.41084317105790391, -0.41642956009763715, -0.42200027079979968,
    -0.42755509343028208, -0.43309381885315196, -0.43861623853852766,
    -0.4441221445704292, -0.44961132965460654, -0.45508358712634384,
    -0.46053871095824, -0.46597649576796618, -0.47139673682599764,
    -0.47679923006332209, -0.48218377207912272, -0.487550160148436,
    -0.49289819222978404, -0.49822766697278181, -0.50353838372571758,
    -0.508830142543107, -0.51410274419322166, -0.51935599016558964,
    -0.524589682678469, -0.52980362468629461, -0.53499761988709715,
    -0.54017147272989285, -0.54532498842204646, -0.55045797293660481,
    -0.55557023301960218, -0.560661576197336, -0.56573181078361312,
    -0.57078074588696726, -0.57580819141784534, -0.58081395809576453,
    -0.58579785745643886, -0.59075970185887416, -0.59569930449243336,
    -0.600616479383869, -0.60551104140432555, -0.61038280627630948,
    -0.61523159058062682, -0.6200572117632891, -0.62485948814238634,
    -0.629638238914927, -0.63439328416364549, -0.63912444486377573,
    -0.64383154288979139, -0.64851440102211244, -0.65317284295377676,
    -0.65780669329707864, -0.66241577759017178, -0.66699992230363747,
    -0.67155895484701833, -0.67609270357531592, -0.680600997795453,
    -0.68508366777270036, -0.68954054473706683, -0.69397146088965389,
    -0.69837624940897292, -0.7027547444572253, -0.70710678118654757,
    -0.71143219574521643, -0.71573082528381859, -0.72000250796138165,
    -0.724247082951467, -0.7284643904482252, -0.73265427167241282,
    -0.73681656887736979, -0.74095112535495922, -0.745057785441466,
    -0.74913639452345937, -0.75318679904361252, -0.75720884650648457,
    -0.76120238548426178, -0.765167265622459, -0.7691033376455797,
    -0.773010453362737, -0.77688846567323244, -0.78073722857209449,
    -0.78455659715557524, -0.78834642762660634, -0.79210657730021239,
    -0.79583690460888357, -0.799537269107905, -0.80320753148064494,
    -0.80684755354379933, -0.81045719825259477, -0.81403632970594841,
    -0.81758481315158371, -0.82110251499110465, -0.82458930278502529,
    -0.8280450452577558, -0.83146961230254524, -0.83486287498638,
    -0.83822470555483808, -0.84155497743689844, -0.84485356524970712,
    -0.84812034480329723, -0.8513551931052652, -0.85455798836540053,
    -0.85772861000027212, -0.86086693863776731, -0.8639728561215867,
    -0.86704624551569265, -0.87008699110871146, -0.87309497841829009,
    -0.8760700941954066, -0.87901222642863353, -0.881921264348355,
    -0.88479709843093779, -0.88763962040285393, -0.89044872324475788,
    -0.89322430119551532, -0.89596624975618522, -0.89867446569395382,
    -0.901348847046022, -0.90398929312344334, -0.90659570451491533,
    -0.90916798309052238, -0.91170603200542988, -0.91420975570353069,
    -0.9166790599210427, -0.91911385169005777, -0.9215140393420419,
    -0.92387953251128674, -0.92621024213831138, -0.92850608047321559,
    -0.93076696107898371, -0.932992798834739, -0.93518350993894761,
    -0.937339011912575, -0.93945922360218992, -0.94154406518302081,
    -0.94359345816196039, -0.94560732538052128, -0.94758559101774109,
    -0.94952818059303667, -0.95143502096900834, -0.95330604035419386,
    -0.95514116830577078, -0.95694033573220882, -0.9587034748958716,
    -0.96043051941556579, -0.96212140426904158, -0.96377606579543984,
    -0.9653944416976894, -0.96697647104485207, -0.96852209427441727,
    -0.970031253194544, -0.97150389098625178, -0.97293995220556018,
    -0.97433938278557586, -0.97570213003852857, -0.97702814265775439,
    -0.97831737071962765, -0.97956976568544052, -0.98078528040323043,
    -0.98196386910955524, -0.98310548743121629, -0.984210092386929,
    -0.98527764238894122, -0.98630809724459867, -0.98730141815785843,
    -0.98825756773074946, -0.989176509964781, -0.99005821026229712,
    -0.99090263542778, -0.99170975366909953, -0.99247953459871,
    -0.9932119492347945, -0.99390697000235606, -0.99456457073425542,
    -0.99518472667219693, -0.99576741446765982, -0.996312612182778,
    -0.99682029929116567, -0.99729045667869021, -0.99772306664419164,
    -0.99811811290014918, -0.99847558057329477, -0.99879545620517241,
    -0.99907772775264536, -0.99932238458834954, -0.99952941750109314,
    -0.99969881869620425, -0.9998305817958234, -0.9999247018391445,
    -0.99998117528260111, -1.0 };

            ix = 0;
            ju = 0;
            iy = 0;
            for (i = 0; i < 1023; i++)
            {
                b_y1[iy].re = x[ix];
                b_y1[iy].im = 0.0;
                iy = 1024;
                tst = TRUE;
                while (tst == TRUE)
                {
                    iy >>= 1;
                    ju ^= iy;
                    if ((ju & iy) == 0)
                    {
                        tst = 1;
                    }
                    else
                    {
                        tst = 0;
                    }
                }

                iy = ju;
                ix++;
            }

            b_y1[iy].re = x[ix];
            b_y1[iy].im = 0.0;
            for (i = 0; i < 1024; i += 2)
            {
                temp_re = b_y1[i + 1].re;
                temp_im = b_y1[i + 1].im;
                b_y1[i + 1].re = b_y1[i].re - b_y1[i + 1].re;
                b_y1[i + 1].im = b_y1[i].im - b_y1[i + 1].im;
                b_y1[i].re += temp_re;
                b_y1[i].im += temp_im;
            }

            iDelta = 2;
            iDelta2 = 4;
            k = 256;
            iheight = 1021;
            while (k > 0)
            {
                for (i = 0; i < iheight; i += iDelta2)
                {
                    iy = i + iDelta;
                    temp_re = b_y1[iy].re;
                    temp_im = b_y1[iy].im;
                    b_y1[i + iDelta].re = b_y1[i].re - b_y1[iy].re;
                    b_y1[i + iDelta].im = b_y1[i].im - b_y1[iy].im;
                    b_y1[i].re += temp_re;
                    b_y1[i].im += temp_im;
                }

                iy = 1;
                for (ix = k; ix < 512; ix += k)
                {
                    i = iy;
                    ju = iy + iheight;
                    while (i < ju)
                    {
                        temp_re = dv1[ix] * b_y1[i + iDelta].re - dv0[ix] * b_y1[i + iDelta].im;
                        temp_im = dv1[ix] * b_y1[i + iDelta].im + dv0[ix] * b_y1[i + iDelta].re;
                        b_y1[i + iDelta].re = b_y1[i].re - temp_re;
                        b_y1[i + iDelta].im = b_y1[i].im - temp_im;
                        b_y1[i].re += temp_re;
                        b_y1[i].im += temp_im;
                        i += iDelta2;
                    }

                    iy++;
                }

                k /= 2;
                iDelta = iDelta2;
                iDelta2 <<= 1;
                iheight -= iDelta;
            }
            //  memcpy(&y[0], &b_y1[0], sizeof(creal_T) << 10);
            b_y1.CopyTo(y, 0);
            //for (int index = 0; index < 1024; index++)
            //{
            //    y[index] = b_y1[index];
            //}

        }


        public static void libfft(float[] a, ref float[] A, ref float[] B)
        {
            creal_T[] b_A = new creal_T[1024];
            Int32 i0;
            FFTCalcnew(a, b_A);
            angle(b_A, B);
            b_abs(b_A, A);
            for (i0 = 0; i0 < 1024; i0++)
            {
                if (i0 == 0)
                {
                    A[i0] /= 1024.0f;
                }
                else
                {
                    A[i0] /= 512.0f;
                }
                B[i0] = (float)(B[i0] * 180.0 / 3.1415926535897931 + 90.0);
            }
        }

        public static void angle(creal_T[] x, float[] y)
        {
            Int32 k;
            for (k = 0; k < 1024; k++)
            {
                y[k] = (float)Math.Atan2(x[k].im, x[k].re);
            }
        }

        public static void b_abs(creal_T[] x, float[] y)
        {
            Int32 k;
            for (k = 0; k < 1024; k++)
            {
                y[k] = (float)Math.Sqrt(x[k].re * x[k].re + x[k].im * x[k].im);
            }
        }

        #endregion

    }
   public struct FFTStruct
   {
       public double[]AMP;
       public double[] Phase;
   }


   public struct creal_T
   {
       public double re;
       public double im;
   }

}
